// wglbase.hpp
//

#ifndef _WGLBASE_HPP
#define _WGLBASE_HPP

#define SDL_MAIN_HANDLED

#include <SDL.h>

#include <filesystem>
#include <string>
#include <vector>
#include <unordered_map>

#ifdef _WIN32
#  ifdef WGL_COMPILE
#    define WGL_API __declspec(dllexport)
#  else
#    define WGL_API __declspec(dllimport)
#  endif
#else
#  define WGL_API
#endif


#define WGL_ENUM_VALUE(v) (static_cast<std::underlying_type<decltype(v)>::type>(v))


#define WGL_LU(v) ((long unsigned int)(v))



// [FIXME] logging
#include <stdio.h>

#define WGL_LOG(f, lvl, msg...) do {            \
    fprintf((f), "wgl:%s:%d:%s ",               \
            __FILE__, __LINE__, (lvl));         \
    fprintf((f), msg);                          \
    fprintf((f), "\n");                         \
  } while (0)

#define WGL_TRACE(msg...) WGL_LOG(stdout, "", msg)

#define WGL_WARNING(msg...) WGL_LOG(stderr, "WARNING:", msg)

#define WGL_ERROR(msg...) WGL_LOG(stderr, "ERROR:", msg)

#define WGL_FATAL(msg...) do {                  \
    WGL_ERROR(msg);                             \
    abort();                                    \
  } while (0)

#define WGL_ASSERT(expr) do {                       \
    if (!(expr)) {                                  \
      WGL_FATAL("ASSERTION FAILED: `" #expr "'");   \
    }                                               \
  } while (0)

#define WGL_CHECK_SDL do {                              \
    const char *_sdl_error = SDL_GetError();            \
    if (_sdl_error[0] != 0) {                           \
      WGL_FATAL("uncaught SDL error: %s", _sdl_error);  \
    }                                                   \
  } while (0)



// [temp]
#include "glm/mat4x4.hpp"

namespace std
{
  inline std::string to_string ( glm::mat4 m )
  {
    std::string s{};
    char buf[16];
    for (int j = 0; j < 4; ++j)
      {
        for (int i = 0; i < 4; ++i)
          {
            snprintf(buf, 16, "%-6.3f  ", m[j][i]);
            s += buf;
          }
        s += "\n";
      }
    return s;
  }
}

#endif
